package com.mysoft.gfyx.marketdevelop.service.impl;

import com.mysoft.framework.common.exception.BusinessLogicException;
import com.mysoft.framework.common.util.MappingUtil;
import com.mysoft.framework.context.api.ContextManager;
import com.mysoft.framework.modeling.dto.*;
import com.mysoft.gfyx.common.time.TimeService;
import com.mysoft.gfyx.marketdevelop.controller.dto.inspect.ApplyRankRequestDto;
import com.mysoft.gfyx.marketdevelop.controller.dto.inspect.ChartRequestDto;
import com.mysoft.gfyx.marketdevelop.controller.dto.inspect.PieResponseDto;
import com.mysoft.gfyx.marketdevelop.mip.GetDeveloperDataService;
import com.mysoft.gfyx.marketdevelop.mip.dto.*;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjCustom.*;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjMetric.*;
import com.mysoft.gfyx.marketdevelop.service.*;
import com.mysoft.gfyx.marketdevelop.service.dto.metric.IndicatorDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

@Service
public class ReportServiceImp implements ReportService {

    @Autowired
    private GetDeveloperDataService getDeveloperDataService;

    @Autowired
    private TimeService timeService;

    @Autowired
    private ContextManager contextManager;

    @Value("${tjdata.metrics.project}")
    private String metricProject;


    public List<Map<String, Object>> applyRank(ApplyRankRequestDto dto) {
        String tenantCode = contextManager.fetchContextInfo().getTenantCode();
        TenantRankRequestDto tenantRankRequestDto = new TenantRankRequestDto();
        tenantRankRequestDto.setTenant_code(tenantCode);
        //当月第一天
        Date startDate = timeService.getMonthFirstDay();
        String startDateStr = String.format("%tY-%tm-%td", startDate, startDate, startDate);
        tenantRankRequestDto.setStart_date(startDateStr);
        SqlApiListResponseDto res;
        if (dto.getType().equals(1)) {
            res = getDeveloperDataService.getUseNumRank(tenantRankRequestDto);
        } else if (dto.getType().equals(2)) {
            res = getDeveloperDataService.getCustomerNumRank(tenantRankRequestDto);
        } else {
            res = getDeveloperDataService.getCluNumRank(tenantRankRequestDto);
        }

        if (res.getData() == null) {
            throw new BusinessLogicException("找不到数据");
        }
        return res.getData();
    }
}
